<?php
namespace app\api\service;

class VerifiedClient 
{
	
	public function __construct($option = array()){
		$this->card = $option['card'];
		$this->name = $option['name'];
		$this->mobile = $option['mobile'];
	}

	public function index(){
		if(empty($this->card) || empty($this->name) || empty($this->mobile)){
			return array('message'=>'参数为空');
		}


		$postCharset = "UTF-8";
		$fileCharset = "UTF-8";

		$host = "https://dm-101.data.aliyun.com";
	    $path = "/rest/161225/zmxy/api/zhima.credit.antifraud.verify.json";
	    $method = "POST";
	    $appcode = config("real.AppCode");
	    $headers = array();
	    array_push($headers, "Authorization:APPCODE " . $appcode);
	    //根据API的要求，定义相对应的Content-Type
	    array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
	    $querys = "";
	    $bodys = "certNo=".$this->card."&certType=IDENTITY_CARD&name=".$this->name."&mobile=".$this->mobile."";
	    $url = $host . $path;

	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	    curl_setopt($curl, CURLOPT_FAILONERROR, false);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_HEADER, true);

	    if (1 == strpos("$".$host, "https://"))
	    {
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    }
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

	    $reponse = curl_exec($curl);

	    if (curl_errno($curl)) {

			throw new Exception(curl_error($curl), 0);
		} else {
			$httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode) {
				throw new Exception($reponse, $httpStatusCode);
			}
		}	 
		curl_close($curl);

		// 将返回结果转换本地文件编码
		$r = iconv($postCharset, $fileCharset . "//IGNORE", $reponse);   
		$curl_json = stripos($r,"{");
		$curl_json = substr($r,$curl_json);	    
		$curl_json = json_decode($curl_json,1);

		return $curl_json;
	}


}